package com.atguigu.mr.outputformat;

import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

import java.io.IOException;

/**
 * @description: xxx
 * @time: 20191229 10:52
 * @author: baojinlong
 **/
@Slf4j
public class FilterRecordWriter extends RecordWriter<Text, NullWritable> {
    private FSDataOutputStream atguiguFSDataOutputStream;
    private FSDataOutputStream otherFSDataOutputStream;

    public FilterRecordWriter(TaskAttemptContext job) {
        try {
            FileSystem fileSystem = FileSystem.get(job.getConfiguration());
            atguiguFSDataOutputStream = fileSystem.create(new Path("D:/home/test/big-data/atguigu.log"));
            otherFSDataOutputStream = fileSystem.create(new Path("D:/home/test/big-data/other.log"));
        } catch (IOException e) {
            log.error("异常");
        }
    }

    @Override
    public void write(Text key, NullWritable value) throws IOException, InterruptedException {
        // http://www.baidu.com
        if (key.toString().contains("atguigu")) {
            atguiguFSDataOutputStream.write(key.toString().getBytes());
        } else {
            otherFSDataOutputStream.write(key.toString().getBytes());
        }
    }

    @Override
    public void close(TaskAttemptContext context) throws IOException, InterruptedException {
        // 关闭资源
        IOUtils.closeStream(atguiguFSDataOutputStream);
        IOUtils.closeStream(otherFSDataOutputStream);
    }
}
